package com.wondertek.service.mapper.business;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wondertek.service.dto.app.req.AppDevicePageByUserIdReq;
import com.wondertek.service.dto.app.res.AppDevicePageByUserIdRes;
import com.wondertek.service.dto.business.req.AccessoryStatusDetectionReq;
import com.wondertek.service.dto.business.req.DevicePageReq;
import com.wondertek.service.dto.business.res.DeviceAllRes;
import com.wondertek.service.dto.business.res.DevicePageRes;
import com.wondertek.service.dto.business.res.SiteDeviceAccessoryRes;
import com.wondertek.service.entity.business.AccessoryPo;
import com.wondertek.service.entity.business.DevicePo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @description: 设备管理
 * @author: qinjianhui
 * @create: 2024-07-18 10:22
 **/
public interface DeviceMapper extends BaseMapper<DevicePo> {

    /*
    * 根据设备id查询配件
    * */
    List<AccessoryPo> selectAccessoryByDeviceId(@Param("id") String id);

    /*
    * 设备列表分页
    * */
    List<DevicePageRes> page(DevicePageReq devicePageReq);

    /*
     * 查询未租赁的设备
     * */
    List<DevicePo> selectNotRentaldevice(DevicePageReq req);

    /*
     * 根据用户ID查询设备
     * */
    List<AppDevicePageByUserIdRes> pageByUserId(AppDevicePageByUserIdReq req);

    /**
     * 根据站点id查询所有网关设备
     * @param siteId
     * @return
     */
    @Select("select d.id deviceId,d.device_name from device d left join device_type_detail dt on d.device_type_id = dt.id  where is_delete = 0 and dt.category = 2 " +
            "and site_id = #{siteId}")
    List<DeviceAllRes> findAllGatewayBySiteId(@Param("siteId")  String siteId);

    /**
     * 查询所有网关设备
     * @param siteId
     * @return
     */
    List<DeviceAllRes> findAllGateway(@Param("siteId")  String siteId,@Param("userId") String userId);

    /*
    * 查询站点设备信息
    * */
    SiteDeviceAccessoryRes selectSiteDevice(DevicePo devicePo);

    /*
     * 查询站点设备配件信息
     * */
    SiteDeviceAccessoryRes selectSiteDeviceAccessory(AccessoryPo accessoryPo);

    @Select("<script> select sn_code,accessory_name,case accessory_type when 1 then 'Smoke detector' when 2 then 'Wireless keyfob' when 3 then 'External Siren' when 4 then 'Door Detector' when 5 then 'Door Detector Plus' when 6 then 'Pir Detertor' else '-' end as accessory_type, (select room_name from room r where r.id = a.room_id) roomName from accessory a where is_delete = 0 and a.sn_code in <foreach collection='accessorySn' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach> </script>")
    List<AccessoryStatusDetectionReq> accessoryStatusDetection(@Param("accessorySn") List<String> sn);

    /**
     * 返回值大于0表示该设备下有布防的分区
     * @param deviceSn
     * @return
     */
    @Select("select count(1) from room where is_delete = 0 and mode !='D' and device_id = (select id from device where sn_code = #{deviceSn} and is_delete = 0)")
    Integer findModeByDeviceSn(@Param("deviceSn") String deviceSn);
}

